package com.dcbeida.activity.applyreturn;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.ApplyReturn;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/10/20.
 */

public class ApplyReturnPresenter implements ApplyReturnContract.Presenter {
    private final ModelUtils model;
    private Activity act;
    ApplyReturnContract.View view;

    public ApplyReturnPresenter(ApplyReturnContract.View view, Activity act) {
        this.act = act;
        this.view = view;
        model = ModelUtils.newInstance();
    }

    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void getSalsesDetailInfo(String uid, String orderid, String patientid) {
        JsonGet jg = new JsonGet().putopt("uid", uid)
                .putopt("orderid", orderid)
                .putopt("patientid", patientid);
        String json = jg.tostring();

        model.getDate("getorderdrugs", json, new GetListCallBack(act, SalsesDetail.class, lis));
    }


    public class GetListCallBack<T> extends DialogCallback<T> {
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((SalsesDetail) data).getList().size() >= 0) {
                    view.showSalsesDetailList(((SalsesDetail) data).getList());
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");
        }
    }

    /**
     *
     * @param orderid
     * @param refundlogid //退货单ID，用于更新时
     * @param quantity
     * @param amount
     * @param drugs
     */
    @Override
    public void submit(String orderid, String refundlogid, String quantity, String amount, String drugs) {
        JsonGet jg = new JsonGet().putopt("uid", MyApplication.user.getUid())
                .putopt("orderid", orderid)
                .putopt("refundlogid", refundlogid)
                .putopt("quantity", quantity)
                .putopt("amount", amount)
                .putopt("drugs", drugs);
        String json = jg.tostring();

        model.getDate("addrefundlog", json, new GetListCallBack2(act, ApplyReturn.class, lis));
    }

    public class GetListCallBack2<T> extends DialogCallback<T> {
        public GetListCallBack2(Activity activity, Type type, DialogInterface.OnCancelListener lis) {
            super(activity, type, lis);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((ApplyReturn) data)!=null) {

                    view.showSuccess(((ApplyReturn) data));
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");
        }
    }

}
